package member.model.dao;

import java.sql.SQLException;
import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import member.domain.Member;
@Repository
public class MemberDAOMybatisImpl implements MemberDAO{
	
	/*
	 * SqlSessionTemplate : mybatis의 SqlSession 역할.
	 * 		- SqlSession의 JDBC 관련 메소드와 같은 메소드들을 제공.
	 * 		- SqlSession과의 차이점 - Transaction 처리(commit/rollback)
	 * 			- SqlSession : 직접처리
	 * 			- SqlSessionTemplate : Spring 컨테이너에게 맞긴다.
	 * 		- 생성자를 통해 SqlSessionFactoryBean을 주입받아야 한다.
	 * 			- SqlSessionFactoryBean - SqlSessionFactory를 생성하는 객체.
	 */
	
	@Autowired
	private SqlSessionTemplate session;
	
	public void setSqlSessionTemplate(SqlSessionTemplate session){
		this.session = session;
	}
	
	@Override
	public int insertMember(Member member) throws SQLException {
		return session.insert(makeSqlId("insertMember"), member);
	}

	@Override
	public int updateMember(Member member) throws SQLException {
		return session.insert(makeSqlId("updateMember"), member);
	}

	@Override
	public int deleteMemberById(String id) throws SQLException {
		return session.insert(makeSqlId("deleteMemberById"), id);
	}

	@Override
	public Member selectMemberById(String id) throws SQLException {
		return session.selectOne(makeSqlId("selectMemberById"), id);
	}

	@Override
	public List<Member> selectMemberList() throws SQLException {
		return session.selectList(makeSqlId("selectMemberList"));
	}

	@Override
	public List<Member> selectMemberByName(String name) throws SQLException {
		return session.selectList(makeSqlId("selectMemberByName"), name);
	}

	private String makeSqlId(String id){
		return "member.manager."+id;
	}

	
}
